using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;
//using GameLogicLibrary;



namespace GameXNA_Master
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class GameLogic : Microsoft.Xna.Framework.GameComponent
    {

        Camera camera;

        RenderingData rendering_data;


        Vector3 direction;

        float TotalelapsedSec = 0;
        float TotalelapsedMillisec = 0;


        public GameLogic(Game game)
            : base(game)
        {
             camera = new Camera();
             Game.Services.AddService(typeof(Camera), camera);
        }

        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(Camera));


            base.Dispose(disposing);
        }



        public override void Initialize()
        {


            rendering_data = (RenderingData)Game.Services.GetService(typeof(RenderingData));
            InputService input_service = (InputService)Game.Services.GetService(typeof(InputService));
            Floor floor = (Floor)Game.Services.GetService(typeof(Floor));

            camera.position = BulletLogic.projectiles_enemies_State.Player.Value.Position.Value + new Vector3(0, 6.5f, -6);

            camera.aspectRatio = rendering_data.aspectRatio;

            camera.setProjMatrix();



            BulletLogic.floorHeight = floor.floorHeight;

            base.Initialize();
        }


        public override void Update(GameTime gameTime)
        {

            TotalelapsedSec += (float)gameTime.ElapsedGameTime.TotalSeconds;
            TotalelapsedMillisec += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 100.0f;

            Vector3 ship_position = BulletLogic.projectiles_enemies_State.Player.Value.Position.Value;

            Matrix forwardMovement = Matrix.CreateFromYawPitchRoll(camera.avatarYaw, camera.avatarPitch, 0);

            direction = -forwardMovement.Forward * 100;
            

            BulletLogic.bullet_rotation = camera.rotMatrix;
            
            //BulletLogic.bullet_position = (camera.cameraLookAt - (forwardMovement.Forward + new Vector3(1.0f, 1.2f, -2))) + direction * 0.02f; // new Vector3(1.2f,1.5f,0) vettore x mettere al centro il bullet

            Vector3 center = Game.GraphicsDevice.Viewport.Unproject(new Vector3(rendering_data.screenWidth/2 +120, rendering_data.screenHeight/2+150, 0.5f), camera.proj, camera.view, camera.world);
            BulletLogic.bullet_position = center;

            BulletLogic.update_state((float)gameTime.ElapsedGameTime.TotalSeconds);
            BulletLogic.update_script();

            
            Casanova.commit_variable_updates();

            camera.position = BulletLogic.projectiles_enemies_State.Player.Value.Position.Value + new Vector3(0, 8f, 0);

            BulletLogic.direction = direction;

           
            


            

            base.Update(gameTime);
        }



    }
}
